package com.fuze.controller.admin;

import com.fuze.common.Advice;
import com.fuze.common.CustomException;
import com.fuze.common.Result;
import com.fuze.domain.dto.AdministratorsDTO;
import com.fuze.domain.vo.AdministratorsVO;
import com.fuze.service.AdministratorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author sepc_
 * @date 4/13/2021 13:17
 */
@Api(tags = "管理员接口")
@Slf4j
@RequiredArgsConstructor
@RestController
public class AdminController {

    final AdministratorsService administratorsService;

    /**
     * 添加一名管理员
     *
     * @param administratorsVO 管理员表单数据
     * @return 是否添加成功
     */
    @ApiOperation(value = "管理员注册（添加管理员）")
    @PostMapping("/manager/register")
    public Result register(@RequestBody @Validated AdministratorsVO administratorsVO) {
        int register = administratorsService.register(administratorsVO.toDTO());
        if (register != 1) {
            throw new CustomException(Advice.REGISTRATION_FAILED);
        }
        return Result.ok();
    }

    @ApiIgnore
    @PostMapping("/manager/update")
    public Result update(@RequestBody @Validated AdministratorsDTO administratorsDTO) {
        int update = administratorsService.update(administratorsDTO);
        if (update != 1) {
            throw new CustomException(Advice.FAILED_UPDATED);
        }
        return Result.ok();
    }

}
